package GUI;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JButton;

import Client.HTSMain;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.ImageIcon;
import java.awt.Cursor;

public class Center extends Menu {
	
	private JTable table;
	public Object o;
	public Object per;
	private int i =0 ; 
	public ArrayList<Object> str;
	public DefaultMutableTreeNode node_1;
	private JLabel ContinueButton;
	
	public Center(ArrayList<Object> msg) {
		btnUpdatescenario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		this.str=msg;
		
		initComponents();	
	}

	
	public void initComponents()
	{
		
		Menu.btnRun.setEnabled(false); //setting buttons
		Menu.StopButton.setEnabled(false);
		Menu.btnAddScen.setEnabled(true);
		Menu.btnAddUser.setEnabled(true);
		Menu.btnDeleteScen.setEnabled(false);
		Menu.btnHome.setEnabled(true);
		Menu.btnStatistic.setEnabled(true);
		Menu.btnUpdatescenario.setEnabled(false);
		Menu.btnScenarioBank.setEnabled(true);
		Menu.btnExit.setEnabled(true);
		
	
		AddScenarioFrame.setLocation(366, 441);
		btnHome.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(312, 152, 672, 278);
		add(scrollPane);

		Vector<Vector<Object>> data = new Vector<Vector<Object>>();
		 

        for (i=1; i<=(this.str.size()-5) ;i=i+5){
        	
        	if (str.get(i).equals("CCR"))
        	{
        		break;
        	}
        	else{
        		  Vector<Object> row = creatrow(str,i);
        	        data.add(row);
        	}
      
        }
	
        Vector<String> headers = new Vector<String>();
        headers.add("Scenario ID");
        headers.add("Scenario Name");
        headers.add("Status");
        
        headers.add("%");
        headers.add("Owner");
        final JTable table = new JTable( data, headers );
        table.setFont(new Font("Tahoma", Font.PLAIN, 14));
        table.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		int col = table.getSelectedColumn();   
                int row = table.getSelectedRow();
                boolean is=table.isColumnSelected(0);
                if (!is){
        			
        			JOptionPane.showMessageDialog(null, "Please select scenario ID only","Error",JOptionPane.ERROR_MESSAGE);

        		}
                else
                {
                	
                	o=((String)table.getValueAt(row, col+1));
                	ContinueButton.setEnabled(true);
                	
                	per=((String)table.getValueAt(row, col+2));
                	ContinueButton.setEnabled(true);
                	
            		
            		
                }
        		
        	}
        });
       // table.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        table.setShowHorizontalLines(true);
        table.setShowVerticalLines(true);
        DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
        centerRenderer.setHorizontalAlignment( JLabel.CENTER );
        JTableHeader header = table.getTableHeader();
        header.setDefaultRenderer(centerRenderer);
        for (int i=0;i<table.getColumnCount();i++)
        {
        	table.getColumnModel().getColumn(i).setCellRenderer( centerRenderer );
        	
        }
        table.setRowHeight(20);
        table.setPreferredScrollableViewportSize(new Dimension(900,150));
        scrollPane.setViewportView(table);
        
        ContinueButton = new JLabel("");
        ContinueButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        ContinueButton.setToolTipText("Continue Run");
        ContinueButton.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		if (per.equals("Finished"))
        		{
    				JOptionPane.showMessageDialog(null, "The Scenario is finished","Info",JOptionPane.INFORMATION_MESSAGE);

        		}
        		else{
        		
        		ArrayList AddArray=new ArrayList();
	 			AddArray.add("ContinueStep");
	 			//AddArray.add("1");
	 			AddArray.add(o);
	 			HTSMain.chat.handleMessageFromGui(AddArray);
        		}
        		
        		
        		
        	}
        });
        ContinueButton.setIcon(new ImageIcon(Center.class.getResource("/Images/Step-Forward-Pressed-Blue-icon.png")));
        ContinueButton.setFont(new Font("Tahoma", Font.PLAIN, 14));
        ContinueButton.setBounds(923, 441, 61, 62);
        add(ContinueButton);
        ContinueButton.setEnabled(false);
        
        JScrollPane TreeScrollPane = new JScrollPane();
        TreeScrollPane.setBounds(26, 152, 250, 513);
        add(TreeScrollPane);
        
       
       
        final JTree tree = new JTree();
        tree.addTreeSelectionListener(new TreeSelectionListener() {
        	public void valueChanged(TreeSelectionEvent arg0) {  //Tree action lestener 
        		
        		DefaultMutableTreeNode selectedNode =  (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
        		
        		if (selectedNode.isLeaf()) //  != "CCR" && selectedNode !="VRS" && selectedNode != "Other" && selectedNode != "Project" )
        		{	
        			if (selectedNode.toString().equals("CCR") || selectedNode.toString().equals("VRS")|| selectedNode.toString().equals("Other") );
        			
        			else{
        				
                		
                    	ArrayList AddArray=new ArrayList();
        	 			AddArray.add("ContinueStep");
        	 			//AddArray.add("1");
        	 			AddArray.add(selectedNode.toString());
        	 			
        	 			HTSMain.chat.handleMessageFromGui(AddArray);
        				
        				}
        		}
        			       
        		
        	}
        });
        TreeScrollPane.setViewportView(tree);
        tree.setFont(new Font("Tahoma", Font.PLAIN, 14));
        tree.setModel(new DefaultTreeModel(
        	new DefaultMutableTreeNode("Project") {
        		{
        			
        			node_1 = new DefaultMutableTreeNode("CCR");
        			
        			for ( i=i+1; i < str.size() ; i++)
        			{
        				if (str.get(i).equals("VRS"))
        	        	{
        	        		break;
        	        	}
        	        	else{
        	        		node_1.add(new DefaultMutableTreeNode(str.get(i)));
        	        		}
        				
        			}
        				
        			add(node_1);
        			node_1 = new DefaultMutableTreeNode("VRS");
        			
        			for (i=i+1; i < str.size() ; i++)
        			{
        				if (str.get(i).equals("Other"))
        	        	{
        	        		break;
        	        	}
        	        	else{
        	        		node_1.add(new DefaultMutableTreeNode(str.get(i)));
        	        		}
        				
        			}
        				//node_1.add(new DefaultMutableTreeNode(""));
        			add(node_1);
        			node_1 = new DefaultMutableTreeNode("Other");
        			for (i=i+1 ; i < str.size() ; i++)
        			{
        			
        	        	//	node_1.add(new DefaultMutableTreeNode(str[i]));
        				node_1.add(new DefaultMutableTreeNode(str.get(i)));
        	        		
        				
        			}
        				//node_1.add(new DefaultMutableTreeNode(""));
        			add(node_1);
        		}
        	}
        ));
        
        JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        lblNewLabel.setToolTipText("Delete Run");
        lblNewLabel.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mousePressed(MouseEvent arg0) {
        		
        		ArrayList AddArray=new ArrayList();
	 			AddArray.add("DeleteRun");
	 			//AddArray.add("1");
	 			AddArray.add(o);
	 			HTSMain.chat.handleMessageFromGui(AddArray);
        		
        		
        	}
        });
        lblNewLabel.setIcon(new ImageIcon(Center.class.getResource("/Images/Close-icon.png")));
        lblNewLabel.setBounds(843, 451, 55, 49);
        add(lblNewLabel);

	
        
        
        
        
	}
	
	
	
	
	
	private Vector<Object> creatrow(ArrayList<Object> str, int i) {
		 Vector<Object> row = new Vector<Object>();
     row.add( ""+str.get(i));
     row.add( ""+str.get(i+1));
     row.add( ""+str.get(i+2));
     row.add( ""+str.get(i+3));
     row.add( ""+str.get(i+4));

		return row;
	}
}
